package Exam2.exam1;


import java.util.Scanner;

public class Chess {
    //单例设计模式
    private static Chess chess = new Chess();
    private Chess() {
    }

    public static Chess getChess() {
        return chess;
    }

    private int[][] qipan = new int[16][16];   //创建一个二维数组存储棋盘
    boolean flag = true;//定义一个boolean类型判断黑方还是白方
    private final int arr[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};//棋盘行列编号

    private void show() {//绘制棋盘
        for (int i = 0; i < arr.length + 1; i++) {//第一行多出一个空格
            if (i == 0) {//第一行特殊处理
                System.out.print("   ");
            } else {
                System.out.printf("%x" + "  ", arr[i - 1]);//打印棋盘的列的序号
            }
        }
        System.out.println();
        for (int i = 0; i < arr.length; i++) {
            System.out.printf("%x" + "  ", arr[i]);//绘制棋盘的每一行时，先输出序号
            for (int j = 0; j < arr.length; j++) {
                if (qipan[i][j] == 1) {//游戏开始时，判断下棋坐标位置，如果是黑棋，则坐标值置为一，并输出$代替黑棋
                    System.out.print("$  ");
                } else if (qipan[i][j] == 2) {//如果是白棋，则坐标值置为二，并输出@代替黑棋
                    System.out.print("@  ");
                } else
                    System.out.print("+  ");//默认值为零，输出+
            }
            System.out.println();

        }

    }

    public void play() {//游戏开始
        while (true) {
            show();
            Scanner sc = new Scanner(System.in);

            if (flag) {
                System.out.println("请输入下棋坐标,现在由黑子落棋");
            } else {
                System.out.println("请输入下棋坐标,现在由白子落棋");
            }
            int x = sc.nextInt();//下棋的横坐标
            int y = sc.nextInt();//下棋的纵坐标
            if (flag) {
                if (check(x, y)) {//判断坐标是否越界
                    qipan[x][y] = 1;
                    flag = !flag;
                }
            } else {
                if (check(x, y)) {//
                    qipan[x][y] = 2;
                    flag = !flag;
                }
            }
            if (win(x, y)) {//判断是否获胜
                if (!flag) {
                    show();
                    System.out.println("黑子获胜");
                } else {
                    show();
                    System.out.println("白子获胜");
                }

                break;

            }
        }

    }

    /**
     *
     * @param x 棋的横坐标
     * @param y 棋的纵坐标
     *
     */
    private boolean check(int x, int y) {//判断坐标是否合法
        if (x < 0 || x > 15 || y < 0 || y > 15) {
            System.out.println("棋盘坐标越界，请重新落子");
            return false;
        } else if (qipan[x][y] != 0) {
            System.out.println("坐标棋子已存在，请重新落子");
            return false;

        }
        return true;
    }

    private boolean win(int x, int y) {//判断是否获胜
        // TODO Auto-generated method stub
        if (line(x, y) || row(x, y) || accossLeft(x, y) || accossRight(x, y)) {//判断如果任意方向五子连珠则获胜
            return true;
        }
        return false;
    }

    private boolean line(int x, int y) {
        int chess = 1;
        for (int i = 1; i <=4; i++) {//向左遍历
            if (y - i >= 0 && qipan[x][y - i] == qipan[x][y]) {
                chess++;
            } else {
                break;
            }

        }

        for (int i = 1; i <=4; i++) {//向右遍历
            if (y + i < 16 && qipan[x][y + i] == qipan[x][y]) {
                chess++;
            } else {
                break;
            }
        }
        if (chess >= 5) {
            return true;
        } else {

            return false;
        }
    }

    private boolean row(int x, int y) {
        int chess = 1;
        for (int i = 1; i <= 4; i++) {//向上遍历
            if (x - i >= 0 && qipan[x - i][y] == qipan[x][y]) {
                chess++;
            } else {
                break;
            }

        }

        for (int i = 1; i <=4; i++) {//向下遍历
            if (x + i < 16 && qipan[x + i][y] == qipan[x][y]) {
                chess++;
            } else {
                break;
            }
        }
        if (chess >= 5) {
            return true;
        } else {

            return false;
        }

    }

    private boolean accossLeft(int x, int y) {
        int chess = 1;
        for (int i = 1; i <=4; i++) {//左上遍历
            if (x - i >= 0 && qipan[x - i][x - i] == qipan[x][y]) {
                chess++;
            } else {
                break;
            }

        }
        for (int i = 1; i <=4; i++) {//右下遍历
            if (x + i < 16 && qipan[x + i][x + i] == qipan[x][y]) {
                chess++;
            } else {
                break;
            }
        }
        if (chess >= 5) {
            return true;
        } else {

            return false;
        }


    }

    private boolean accossRight(int x, int y) {
        int chess = 1;
        for (int i = 1; i <=4; i++) {//左下遍历
            if (x + i < 16 && y - i >=0 && qipan[x + i][y - i] == qipan[x][y]) {
                chess++;


            } else {
                break;
            }

        }
        for (int i = 1; i <=4; i++) {//右上遍历
            if (x - i >= 0 && y + i < 16 && qipan[x - i][y + i] == qipan[x][y]) {
                chess++;

            } else {
                break;
            }
        }
        if (chess >= 5) {
            return true;
        } else {

            return false;
        }
    }

}
